package com.weather.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import com.weather.util.XmlBuilder;
import com.weather.vo.City;
import com.weather.vo.CityList;

/**
 * @Description 城市数据接口实现类
 *
 * @author 堆堆堆
 * @date 2018年5月31日 下午5:21:53
 */
@Service
public class CityDataServiceImpl implements CityDataService {

	@Override
	public List<City> listCity() throws Exception {

		// 获取 Xml 文件
		Resource resource = new ClassPathResource("citylist.xml");
		BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
		StringBuffer buffer = new StringBuffer();
		String line = "";

		// 读取内容
		while ((line = br.readLine()) != null) {
			buffer.append(line);
		}
		br.close();

		// Xml 转化为 Java 对象
		CityList cityList = XmlBuilder.xmlStrToObject(CityList.class, buffer.toString());
		return cityList.getCityList();
	}

}
